import Utils from '../utils/index';

/**
 * @desc: password des key
 * @return: string
 */
const pwdKey = '6E7E0AE69DF14EA9060A8E9040D7AFDB';

/**
 * 获取加密后的key或者value
 * @param key
 * @returns {string}
 */
const getEncryptKey = key => {
  return Utils.desEncrypt(key, pwdKey);
};

/**
 * 获取解密后的key或者value
 * @param key
 * @returns {string}
 */
const getDecryptKey = key => {
  return Utils.desDecrypt(key, pwdKey);
};

/**
 * 往本地存储存值
 * @param key
 * @param value
 */
const setItem = (key, value) => {
  let tempKey = getEncryptKey(key);
  let tempValue = value;
  if (tempValue === undefined || tempValue === null) {
    tempValue = '';
  }
  tempValue = getEncryptKey(tempValue);
  window.localStorage.setItem(tempKey, tempValue);
};

/**
 * 从本地存储取值
 * @param key
 */
const getItem = key => {
  let tempKey = getEncryptKey(key);
  let result = window.localStorage.getItem(tempKey);
  result = result ? getDecryptKey(result) : '';
  return result;
};

/**
 * 设置计算sign值的key
 */
const setSignKey = value => {
  let tempKey = getEncryptKey('PRIMARY_0');
  let tempValue = value;
  if (tempValue === undefined || tempValue === null) {
    tempValue = '';
  }
  window.localStorage.setItem(tempKey, tempValue);
};

/**
 * 获取计算sign值的key
 */
const getSignKey = () => {
  let tempKey = getEncryptKey('PRIMARY_0');
  return window.localStorage.getItem(tempKey);
};

// 密码规则 6-18位字母，数字，特殊符号组成，字母区分大小写
const passWordReg =
  /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[-`_~!@#$%^&*.()[\]{}<>?\\/'"+;:,|=])[\da-zA-Z\-`_~!@#$%^&*.()[\]{}<>?\\/'"+;:,|=]{6,18}$/;

export default {
  passWordReg,
  setSignKey,
  getSignKey,
  setItem,
  getItem
};
